﻿using Firefly.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Firefly.Framework.Components
{
    public sealed class QuadRenderer : DrawableComponent
    {
        private VertexDeclaration _vertexDeclaration = null;
        private VertexPositionTexture[] _vertexBuffer = null;
        private short[] _indexBuffer = null;

        public bool FullScreen { get; set; }

        public QuadRenderer(Game game)
            : this(game, null)
        {
        }

        public QuadRenderer(Game game, VertexPositionTexture[] vertexBuffer)
            : base(game)
        {
            _vertexBuffer = vertexBuffer;
            FullScreen = true;
        }

        #region IHasContent Members

        public override void LoadContent()
        {
            _vertexDeclaration = new VertexDeclaration(GraphicsService.Instance.GraphicsDevice, VertexPositionTexture.VertexElements);

            if (_vertexBuffer == null)
            {
                _vertexBuffer = new VertexPositionTexture[]
                {
                    new VertexPositionTexture(new Vector3(0, 0, 0), new Vector2(1, 1)),
                    new VertexPositionTexture(new Vector3(0, 0, 0), new Vector2(0, 1)),
                    new VertexPositionTexture(new Vector3(0, 0, 0), new Vector2(0, 0)),
                    new VertexPositionTexture(new Vector3(0, 0, 0), new Vector2(1, 0))
                };

                FullScreen = true;
            }

            _indexBuffer = new short[] { 0, 1, 2, 2, 3, 0 };
        }

        public override void UnloadContent()
        {
            if (!Disposed)
            {
                if (_vertexDeclaration != null)
                {
                    _vertexDeclaration.Dispose();
                    _vertexDeclaration = null;
                }
            }
        }

        #endregion

        #region IUpdateable Members

        public override void Update(GameTime gameTime)
        {
        }

        #endregion

        #region IDrawable Members

        public override void Draw(GameTime gameTime)
        {
            if (Visible)
            {
                if (FullScreen)
                {
                    Vector2 vector1 = Vector2.One * -1;
                    _vertexBuffer[0].Position.X = Vector2.One.X;
                    _vertexBuffer[0].Position.Y = vector1.Y;
                    _vertexBuffer[1].Position.X = vector1.X;
                    _vertexBuffer[1].Position.Y = vector1.Y;
                    _vertexBuffer[2].Position.X = vector1.X;
                    _vertexBuffer[2].Position.Y = Vector2.One.Y;
                    _vertexBuffer[3].Position.X = Vector2.One.X;
                    _vertexBuffer[3].Position.Y = Vector2.One.Y;
                }

                GraphicsService.Instance.VertexDeclaration = _vertexDeclaration;
                GraphicsService.Instance.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, _vertexBuffer, 0, 4, _indexBuffer, 0, 2);
            }
        }

        #endregion

        #region IDisposable Members

        protected override void Dispose(bool disposing)
        {
            if (!Disposed)
            {
                if (disposing)
                {
                    UnloadContent();
                }

                Disposed = true;
            }
        }

        ~QuadRenderer()
        {
            Dispose(false);
        }

        #endregion
    }
}